<?php
class WCase extends Athena {
	function Edit()
	{
		$backurl = get_cookie("SYSTEM_HTTP_REFERER");
		$id		 = 0;
		if(isset($_GET["id"]) && ereg("^[0-9]+$",$_GET["id"]))
			$id		= $_GET["id"];
		else
			$this->halt("没有选择需要编辑的记录",'ERR');
		?>
			<table Class="yc" cellspacing="1" cellpadding="0">
			  <?php $this->Add_Form($id);?>
			   <tr bgcolor="#FFFFFF">
				<td colspan="2" height="50" style="text-align:center">
					<?$this->Edit_Button();?>
					<input type="hidden" name="SYSTEM_HTTP_REFERER" value="<?=$backurl?>">
					<input type="hidden" name="ID" value="<?=$id?>">
				</td>
			  </tr>
			  <?if($this->remark != "") print("<tr><td colspan=2>{$this->remark}</td></tr>");
			  $this->pic_list($id);
			  ?>
			  
		<br>
		<? $this->CheckForm();
	}
	
	function pic_list($id){
		$list = $this->db->query_row("select * from case_img where pid ='{$id}' order by idx,id",MYSQL_ASSOC);
		?>
		<tr>
			    <td colspan="2">
			    <table Class="yc" cellspacing="1" cellpadding="0" style="width:100%;margin-top:0px;">
			      <tr>
			        <th>上传案例图片</th>
                    <th>图片预览</th>
			        <th>权重</th>
			        <th width="120" align="center">操作</th>
			      </tr>
			      <?php;
			      $html = "";
			      while (list(,$val) = each($list)){
			      	$html .= "<tr><td><input name='picsrc' id='picsrc{$val['id']}' type='file' ></td>";
			        $html .= "<td><a href='{$val['img']}' target='_blank'><img src='{$val['img']}' width='32' height='32'></td>";
			      /*$html .= "<td><input type='text' name='pictitle' value='{$val['title']}' ></td>";
					$html .= "<td><textarea class='news' name='piccontent'>{$val['content']}</textarea></td>";*/
			        $html .= "<td><input type='text' name='picidx' value='{$val['idx']}' maxlength='3'></td>";
			        $html .= "<td><input type='button' name='picsave'  value='保存'>&nbsp;";
			        $html .= "<input type='button' name='picdelete' value='删除'>";
			       	$html .= "<input type='hidden' name='picid' value='{$val['id']}'></td></tr>";
			      }
			      echo $html;
			      ?>
			      <tr>
			        <td><input name="picsrc" id="picsrc" type="file" ></td>
                    <td><a target='_blank'><img src='' width='32' height='32'></td>
			        <!--<td><input type="text" name="pictitle" value="" ></td>
                    <td><textarea class='news' name="piccontent"></textarea></td>-->
			        <td><input type="text" name="picidx" value="1" maxlength="3"></td>
			        <td>
			        	<input type="button" name="picsave"  value="上传">
			        	<input type="hidden" name="picid" value="0">
			        </td>
			      </tr>
			    </table>
			    </td>
			  </tr>
			</table>
		<?php
	}
	
	
}
?>
<script type="text/javascript" src="js/ajaxfileupload.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	$("input[type=button][name=picdelete]").live("click",function(){
		var tr = $(this).parent().parent();
		if(window.confirm("确信删除？")){
			var ID = $("input[name=ID]").val();//产品ID
			var picid = $(tr).find("input[name=picid]").val();//图片自动编号
			var src = $(tr).find("a").attr("href");
			var data = {ID:ID,picid:picid,src:src,cmd:'delete'};
			$.ajax({
				type:'POST',
				data:data,
				url:'../include/ajax_case_pic.php', 
				dataType:'text',
				success:function(msg){
					if(msg == ''){
						$(tr).remove();
					}
					else{
						alert(msg);
					}
				}
			})
		}
	});
	$("input[type=button][name=picsave]").live("click",function(){
		var tr = $(this).parent().parent();
		var idx = $(tr).find("input[name=picidx]").val();//权重
		var ID = $("input[name=ID]").val();//产品ID
		var picid = $(tr).find("input[name=picid]").val();//图片自动编号
		var fileId = $(tr).find("input[type=file]").attr("id");//图片文本框ID
		var filename = $(tr).find("input[type=file]").attr("name");//图片框名称
		/*var title =$(tr).find("input[name=pictitle]").val();//图片标题
		var content =$(tr).find("input[name=piccontent]").val();//内容*/
		var data = {ID:ID,picid:picid,idx:idx,filename:filename};
		picid = parseInt(picid);
		if(picid == 0 && $(tr).find("input[type=file]").val() == '') {
			alert("选择需要上传的图片");
			return;
		}
		$.ajaxFileUpload ({
			url:'../include/ajax_case_pic.php', //你处理上传文件的服务端
			data:data,
			secureuri:false,
			fileElementId:fileId, //与页面处理代码中file相对应的ID值
			dataType: 'json', //返回数据类型:text，xml，json，html,scritp,jsonp五种
	 		success: function (msg) {
	 		 	if(msg != null && msg.no == "0"){
	 		 		if(picid > 0){
	 		 			alert("保存成功");	
	 		 		}
	 		 		else{
		 		 		var url = msg.msg;
		 		 		var html = "<tr><td><input name='picsrc' id='picsrc"+msg.picid+"' type='file' ></td>";
				        html += "<td><a href='"+msg.msg+"' target='_blank'><img src='"+msg.msg+"' width='32' height='32'></td>";
				        /*html += "<td><input type='text' name='pictitle' value='"+title+"' ></td>";
						html += "<td><input type='text' name='piccontent' value='"+content+"' ></td>";*/
				        html += "<td><input type='text' name='picidx' value='"+idx+"' maxlength='3'></td>";
				        html += "<td><input type='button' name='picsave'  value='保存'>&nbsp;";
				        html += "<input type='button' name='picdelete' value='删除'>";
				       	html += "<input type='hidden' name='picid' value='"+msg.picid+"'></td></tr>";
				       	$(html).insertBefore($(tr));
				       	/*$(tr).find("input[name=pictitle]").val('');
						$(tr).find("input[name=piccontent]").val('');*/
				       	$(tr).find("input[name=picidx]").val(parseInt(idx)+1);
	 		 		}
	 		 	}
	 		 	else{
	 		 		alert(msg.msg);
	 		 	}
			}
	    })
	})
})
</script>

